package com.zu.commu.local.process;

import com.zu.commu.context.Config;
import com.zu.commu.interf.Action;
import com.zu.commu.interf.Command;
import com.zu.commu.interf.Order;
import com.zu.commu.interf.ReLocal;
import com.zu.commu.interf.gprs.Engine;
import com.zu.commu.interf.gprs.Handle;
import org.apache.log4j.Logger;

public class LocalGprsProcess
{
  private static Logger log = Logger.getLogger(
    "com.automic.commu.local.process.LocalGprsProcess");

  public String processDataAndSendByThread(String gprsId, Command command, Order order, byte[] bs)
  {
    Handle handle = getHandle(gprsId);
    if (handle == null)
    {
      String error = "没有得到GPRS ID 为" + gprsId + "的GPRS的协议。可能配置的GPRS ID与实际的不相附合,也可能通信服务器现在没有得到对应这个GPRS ID的GPRS通信协议！";
      log.error(error);
      return error;
    }

    String str = null;
    try {
      Action action = handle.analyseOrder(order, command, bs, gprsId);

      if (Action.SENDREMOTE.equals(action))
      {
        byte[] buf = handle.getRemoteData();
        sentRemoteByThread(gprsId, command, order, buf);
      } else if (Action.ERROR.equals(action)) {
        log.error(handle.getError());
        str = handle.getError();
      }

      str = ReLocal.ECHO;
    }
    catch (Exception localException)
    {
    }

    return str;
  }

  public String processDataAndSendDirectSingle(String gprsId, Command command, Order order, byte[] bs)
  {
    Handle handle = getHandle(gprsId);
    if (handle == null)
    {
      String error = "没有得到GPRS ID 为" + gprsId + "的GPRS的协议。可能配置的GPRS ID与实际的不相附合,也可能通信服务器现在没有得到对应这个GPRS ID的GPRS通信协议！";
      log.error(error);
      return error;
    }

    String str = null;
    try {
      Action action = handle.analyseOrder(order, command, bs, gprsId);

      if (Action.SENDREMOTE.equals(action))
      {
        byte[] buf = handle.getRemoteData();
        sentRemoteDirectSingle(gprsId, command, order, buf);
      } else if (Action.ERROR.equals(action)) {
        log.error(handle.getError());
        str = handle.getError();
      }

      str = ReLocal.ECHO;
    }
    catch (Exception localException)
    {
    }

    return str;
  }

  public String processDataAndSendDirect(String gprsId, Command command, Order order, byte[] bs)
  {
    Handle handle = getHandle(gprsId);
    if (handle == null)
    {
      String error = "没有得到GPRS ID 为" + gprsId + "的GPRS的协议。可能配置的GPRS ID与实际的不相附合,也可能通信服务器现在没有得到对应这个GPRS ID的GPRS通信协议！";
      log.error(error);
      return error;
    }
    Action action = handle.analyseOrder(order, command, bs, gprsId);

    if (Action.SENDREMOTE.equals(action))
    {
      byte[] buf = handle.getRemoteData();
      sentRemoteDirect(gprsId, command, order, buf);
    } else if (Action.ERROR.equals(action)) {
      log.error(handle.getError());
      return handle.getError();
    }

    return ReLocal.ECHO;
  }

  private void sentRemoteByThread(String gprsId, Command command, Order order, byte[] buf)
  {
    SendRemote send = new SendRemote();
    send.setSendData(gprsId, command.getCommandId(), buf, order);
    send.start();
  }

  private void sentRemoteDirectSingle(String gprsId, Command command, Order order, byte[] buf)
  {
    SendRemote send = new SendRemote();
    send.setSendData(gprsId, command.getCommandId(), buf, order);
    send.sendSingle();
  }

  private void sentRemoteDirect(String gprsId, Command command, Order order, byte[] buf)
  {
    SendRemote send = new SendRemote();
    send.setSendData(gprsId, command.getCommandId(), buf, order);
    send.send();
  }

  private Handle getHandle(String gprsId)
  {
    Handle h = null;
    try
    {
      Config cf = Config.instance();
      String protocol = cf.getGPRSProcotolOfGPRSID(gprsId);
      if (protocol != null)
      {
        Engine engine = cf.getDriverOfGPRSProtocol(protocol);
        h = engine.getHandle();
      }
    }
    catch (Exception localException)
    {
    }

    return h;
  }
}